package com.stlm2.dems.service;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.entity.FlowSource;
import com.stlm2.dems.entity.FlowType;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.User;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.FmsFlow;
import com.stlm2.dems.dao.FmsFlowDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("fmsFlowService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class FmsFlowService extends BaseServiceImpl<FmsFlow> {

	@Resource
	private FmsFlowDao fmsFlowDao;
	@Resource
	private UserDao userDao;


	@Override
	protected BaseDao <FmsFlow> getDao() {
		return fmsFlowDao;
	}
	/*
	*前台获取当前用户资金流水信息
	* page:页码
	* token：用户token
	 */
	public String getUserFmsFlow(ModelMap model,String page,String token){
		PageView pageview =new  PageView();
		int pageNo = 1;
		int pageSize=20;
		if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
			pageNo = NumberUtils.toInt(page);
		}
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		FmsFlow fmsFlow=new FmsFlow();
		fmsFlow.setUserId(loginUser.getId());
		pageview=fmsFlowDao.queryPageView(pageNo, pageSize,"getByGroupTime",fmsFlow);

		return CodeContent.getModelByCode(1,"success",convert_date(pageview.getRecords()));
	}
	/*
*前台获取当前用户资金流水信息V1.1
* page:页码
* token：用户token
 */
	public String getUserFmsFlowV1(ModelMap model,String page,String token){
		PageView pageview =new  PageView();
		int pageNo = 1;
		int pageSize=20;
		if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
			pageNo = NumberUtils.toInt(page);
		}
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		FmsFlow fmsFlow=new FmsFlow();
		fmsFlow.setUserId(loginUser.getId());
		pageview=fmsFlowDao.queryPageView(pageNo, pageSize,fmsFlow);
		return CodeContent.getModelByCode(1,"success",convert_v1(pageview.getRecords()));
	}

	public List<ModelMap> convert_date(List<FmsFlow> fmsFlows) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (fmsFlows != null && fmsFlows.size() > 0) {
			for (FmsFlow fmsFlow : fmsFlows) {
				ModelMap map = buildModel_date(fmsFlow);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModel_date(FmsFlow fmsFlow) {
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
		ModelMap model = new ModelMap();
		PageView pageview1 =new  PageView();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("createTime",dateFormatter.format(fmsFlow.getCreateTime()));
		map.put("userId",fmsFlow.getUserId());
		pageview1=fmsFlowDao.queryPageView(-1, -1,"getByUserTime",map);

		model.put("createDate",dateFormatter.format(fmsFlow.getCreateTime()));
		model.put("userId",fmsFlow.getUserId());
		model.put("details",convert_v1(pageview1.getRecords()));

		return model;
	}

	public List<ModelMap> convert_v1(List<FmsFlow> fmsFlows) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (fmsFlows != null && fmsFlows.size() > 0) {
			for (FmsFlow fmsFlow : fmsFlows) {
				ModelMap map = buildModel_v1(fmsFlow);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModel_v1(FmsFlow fmsFlow) {
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm");
		ModelMap model = new ModelMap();
		model.put("Id",fmsFlow.getId());
		model.put("Change_num",fmsFlow.getType().toString() + fmsFlow.getChange_num());
		model.put("Source",fmsFlow.getSource().toString());
		model.put("CreateTime",timeFormatter.format(fmsFlow.getCreateTime()));
		model.put("CreateDate",dateFormatter.format(fmsFlow.getCreateTime()));
		model.put("ReferCode",fmsFlow.getReferCode());

		return model;
	}

	public void addFmsFlow(Integer userId,String code, BigDecimal toMoney, FlowSource flowSource, FlowType flowType) {
		FmsFlow fmsFlow=new FmsFlow();
		fmsFlow.setCreateTime(new Date());
		fmsFlow.setChange_num(toMoney);
		fmsFlow.setUserId(userId);
		fmsFlow.setReferCode(code);
		fmsFlow.setSource(flowSource);
		fmsFlow.setType(flowType);
		fmsFlowDao.add(fmsFlow);
	}
}